﻿#ifndef M_FHS_H
#define M_FHS_H

#define TD_TICKET "td_ticket"

#define SZ_CANCEL "1000"    //取消关机
#define SZ_SHUTDOWN "1001"  //立即关机
#define SZ_REBOOT "1010"    //立即重启
#define SZ_HALT_DELAY "1011"    //一分钟后关机

#define SZ_OK "0001"    //确定,发送回车键
#define SZ_ESC "0010"   //取消，发送ESC键
#define SZ_YES "0100"   //是
#define SZ_NO "0101"    //否

#define SZ_SEARCH "2000"    //搜索

#include <QtGlobal>
#include <QProcess>
#include <QString>

static bool do_filter_for_windows(const QString &ticket)
{
    if(ticket == SZ_SHUTDOWN)
    {
        QProcess::startDetached("shutdown /p");
        return false;
    }
    else if(ticket == SZ_REBOOT)
    {
        QProcess::startDetached("shutdown /r /t 0");
        return false;
    }
    else if (ticket == SZ_HALT_DELAY){
        QProcess::startDetached("shutdown /s");
        return false;
    }
    else if(ticket == SZ_CANCEL)
    {
        QProcess::startDetached("shutdown /a");
        return false;
    }
    else
        return true;
}

// shutdown -h | -r 时间单位为分钟，默认 +1
static bool do_filter(const QString &ticket)
{
    if(ticket == SZ_SHUTDOWN)
    {
        QProcess::startDetached("shutdown -h now");     //halt -p
        return false;
    }
    else if(ticket == SZ_REBOOT)
    {
        QProcess::startDetached("shutdown -r now");     //reboot
        return false;
    }
    else if (ticket == SZ_HALT_DELAY){
        QProcess::startDetached("shutdown -h");
        return false;
    }
    else if(ticket == SZ_CANCEL)
    {
        QProcess::startDetached("shutdown -c");
        return false;
    }
    else
        return true;
}

static bool control_filter(const QString &ticket)
{
#ifdef Q_OS_WIN32
    return do_filter_for_windows(ticket);
#else
    return do_filter(ticket);
#endif
}

#endif // M_FHS_H
